<template>
  <div class="table-pagination-wrapper">
    <div class="filter-wrapper">
      <el-form
        :inline="true"
        :model="formModel"
        ref="formInsRef"
        :rules="rules"
      >
        <el-form-item label="名称" prop="label">
          <el-input v-model="formModel.label" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="formModel.value" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="formModel.type" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item>
          <el-button @click="onReset">重 置</el-button>
          <el-button type="primary" @click="onSubmit">查 询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="action-wrapper">
      <el-button type="primary" @click="eventHandler('ADD')">新增</el-button>
    </div>
    <div class="table-wrapper" v-loading="loading">
      <el-table
        :data="tableData"
        stripe
        border
        style="width: 100%"
        max-height="calc(100% - 56px)"
      >
        <el-table-column prop="label" label="名称" show-overflow-tooltip />
        <el-table-column prop="value" label="值"></el-table-column>
        <el-table-column prop="type" label="类型"></el-table-column>
        <el-table-column prop="order" label="排序"></el-table-column>
        <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column fixed="right" label="操作" width="140">
          <template #default="{ row }">
            <el-button
              link
              type="primary"
              size="small"
              @click="eventHandler('DETAIL', row)"
            >
              详情
            </el-button>
            <el-button
              link
              type="primary"
              size="small"
              @click="eventHandler('EDIT', row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              title="确认要删除改行记录吗?"
              width="200"
              @confirm="eventHandler('DELETE', row)"
            >
              <template #reference>
                <el-button link type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="pagination.sizes"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChangeHandler"
          @current-change="handleCurrentChangeHandler"
        />
      </div>
    </div>
  </div>
  <Detail ref="DetailRef" />
  <Form ref="FormRef" @success="init" />
</template>

<script lang="js" setup>
import { ref, onMounted, reactive } from "vue"
import { dictDeleteApi, dictApi, dictDetailApi } from "@/api/dict"
import { delayFn, filterEmptyAttrByObj } from "@/utils"
import { ElMessage } from "element-plus"
import Detail from "./components/Detail.vue"
import Form from "./components/Form.vue"

const formInsRef = ref()
const validateFn = (rule, value, callback) => {
  callback()
}
const rules = reactive({
  label: [{ validator: validateFn }],
  value: [{ validator: validateFn }],
  type: [{ validator: validateFn }],
})
const formModel = reactive({
  label: "",
  value: "",
  type: "",
})

const pagination = ref({
  size: 20,
  page: 1,
  total: 0,
  sizes: [10, 20, 50, 100, 200, 300, 400],
})
const loading = ref(false)
const tableData = ref([])

const init = async () => {
  if (loading.value) {
    return
  }
  loading.value = true
  try {
    const res = await dictApi(
      { ...filterEmptyAttrByObj(formModel) },
      {
        page: pagination.value.page,
        size: pagination.value.size,
      },
    )
    const { count = 0, rows = [] } = res || {}
    pagination.value.total = count
    tableData.value = rows
  } catch (error) {}
  await delayFn()
  loading.value = false
}

const handleSizeChangeHandler = size => {
  pagination.value.page = 1
  pagination.value.size = size
  init()
}

const handleCurrentChangeHandler = page => {
  pagination.value.page = page
  init()
}

const reset = () => {
  Object.assign(pagination.value, { page: 1, size: 20 })
}

const FormRef = ref()
const addHandler = async row => {
  FormRef.value.open()
}

const editHandler = async row => {
  FormRef.value.open({ row })
}

const DetailRef = ref()
const detailHandler = async row => {
  const info = await dictDetailApi(row?.id)
  DetailRef.value.open({ row: info })
}

const deleteHandler = async row => {
  await dictDeleteApi(row?.id)
  ElMessage.success("操作成功")
  init()
}

const eventHandler = async (action, row) => {
  switch (action) {
    case "ADD":
      addHandler()
      break
    case "DETAIL":
      detailHandler(row)
      break
    case "EDIT":
      editHandler(row)
      break
    case "DELETE":
      deleteHandler(row)
      break
    default:
  }
}

const onSubmit = () => {
  console.log("submit!")
  init()
}
const onReset = () => {
  if (!formInsRef.value) return
  formInsRef.value?.resetFields()
  init()
}

onMounted(() => {
  reset()
  init()
})
</script>

<style lang="less" scoped>
.table-pagination-wrapper {
  height: 100%;
  box-sizing: border-box;

  .action-wrapper {
    height: 56px;
    display: flex;
    align-items: center;
  }

  .filter-wrapper {
  }

  .table-wrapper {
    // height: calc(100% - 56px);
  }

  .pagination-wrapper {
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>
